import 'package:dartkit/dartkit.dart';
import 'package:test/test.dart';

void main() {
  test("create创建测试", () {
    // 生成一个正常的sms和过期的sms，发现过期的sms确实已经过期了
    final sms = Sms.create(expireTime: durationOneSecond);
    final smsExpired = Sms.create(expireTime: Duration.zero);
    expect(sms.hasExpired, isFalse);
    expect(smsExpired.hasExpired, isTrue);
    // 它们的创建时间都差不多在现在附近
    expect(sms.createAt.isNear(DateTime.now()), isTrue);
    // sms验证码不为空
    expect(sms.code.isNotEmpty, isTrue);
  });

  test("Raw的创建与恢复测试", () {
    // 创建一个sms，序列化后反序列化
    final sms = Sms.create(expireTime: durationOneSecond);
    final raw = sms.toJson();
    final recover = Sms.fromRaw(raw);
    // 得到的结果都是一致的
    expect(sms.code, recover.code);
    expect(sms.createAt, recover.createAt);
    expect(sms.expireAt, recover.expireAt);
  });

  test("Sms验证码检验", () {
    final sms = Sms(
        code: "0000",
        expireAt: DateTime.now().add(durationOneSecond),
        createAt: DateTime.now());
    final smsExpired =
        Sms(code: "0000", expireAt: DateTime.now(), createAt: DateTime.now());
    expect(() => SmsKit.validate(null, "1234"), isBug); // 验证码对象为空
    expect(() => SmsKit.validate(smsExpired, "0000"), isBug); // 验证码过期
    expect(() => SmsKit.validate(sms, "0123"), isBug); // 验证码错误
  });
}
